package com.yingxin.yxerp.module.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yx.framework.render.core.RenderColumn;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.sys.entity.ActivitiCoreInformation;
import com.yingxin.yxerp.module.sys.mapper.ActivitiCoreInformationMapper;
import com.yingxin.yxerp.module.sys.service.IActivitiCoreInformationService;
import com.yingxin.yxerp.module.sys.vo.req.ActivitiCoreInformationQueryVo;
import com.yingxin.yxerp.module.sys.vo.resp.ActivitiCoreInformationRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 任务核心内容列表 服务类
 * @author yuebao
 * @since 2024-02-24
 */

@Service
public class ActivitiCoreInformationServiceImpl<T>
        extends ServiceImpl<ActivitiCoreInformationMapper, ActivitiCoreInformation>
        implements IActivitiCoreInformationService<T> {

    @Autowired
    private IUserSupport userSupport;

    /**
     * 获取 mapper
     */
    @Override
    public ActivitiCoreInformationMapper mapper() {
        return baseMapper;
    }

    /**
     * 创建
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(String businessId, String businessType, T vo) {
        if(StringUtils.isEmpty( businessId ) ){
            return;
        }
        if(StringUtils.isEmpty( businessType ) ){
            return;
        }
        if( ObjectUtil.isEmpty( vo ) ){
            return;
        }

        Field[] fields = vo.getClass().getDeclaredFields();
        if( ObjectUtil.isEmpty( fields ) ) {
            return;
        }

        List< ActivitiCoreInformation > list = new ArrayList<>();

        try{
            for(Field field : fields) {
                field.setAccessible(true);
                if (field.isAnnotationPresent( RenderColumn.class ) ) {
                    Object obj = field.get( vo );
                    // 是否有值
                    if ( !Objects.isNull(obj) ){
                        // 是否在app中显示
                        if( field.getAnnotation(RenderColumn.class).isToDoList() ){
                            ActivitiCoreInformation activitiCoreInformation = new ActivitiCoreInformation();
                            activitiCoreInformation.setId( IdHelper.snowflakeId() );
                            activitiCoreInformation.setBusinessId( businessId );
                            activitiCoreInformation.setBusinessType( businessType );
                            activitiCoreInformation.setLabel( field.getAnnotation( RenderColumn.class ).name() );
                            activitiCoreInformation.setValue( field.get( vo ).toString() );
                            Integer sort = field.getAnnotation( RenderColumn.class ).sort();
                            if( sort == null ){
                                sort = list.size() + 1;
                            }
                            activitiCoreInformation.setSort( sort );
                            activitiCoreInformation.setCreatorId( userSupport.getUserId() );
                            activitiCoreInformation.setCreateTime( new Date() );

                            list.add( activitiCoreInformation );
                        }
                    }
                }
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
        if(CollectionUtil.isEmpty( list ) ){
            return;
        }

        saveBatch( list );
    }

    /**
     * 查询全部
     */
    @Override
    public List<ActivitiCoreInformationRespVo> listVo(ActivitiCoreInformationQueryVo queryVo) {
        return baseMapper.listVo( queryVo );
    }

    /**
     * 根据 bill id 获取
     */
    @Override
    public List<ActivitiCoreInformationRespVo> listByBillId( String businessId ){
        return baseMapper.listByBillId( businessId );
    }

    /**
     * 根据id查询
     */
    @Override
    public ActivitiCoreInformationRespVo getVo(String id) {
        return baseMapper.getVo( id );
    }

    /**
     * 根据 业务id 删除
     */
    @Override
    public void deleteByBusinessId( String businessId ){
        QueryWrapper<ActivitiCoreInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq( ActivitiCoreInformation::getBusinessId, businessId );
        remove( queryWrapper );
    }

}
